#!/usr/bin/env python

import os, sys
import mad
import glob
import getpass
import math
import csv

def executecmd(cmdstring):
	print cmdstring
	os.system(cmdstring)
	
arglen = len(sys.argv)
 
if arglen != 3:
    print "Usage: csvtrim.py sourcefile csvfile"
    sys.exit(1)

sourcefilepath = sys.argv[1]
csvpath = sys.argv[2]

csvreader = csv.reader(open(csvpath))

for row in csvreader:
	if len(row) < 7:
		print "Row too short: ".join(row)
		sys.exit(1)

	startsecs = row[5]
	lengthsecs = row[6]
	outfile = ("%sraw.mpg") % (row[0])
	
	if outfile == "*stop*":
		print("*stop* found")
		break

	print (("OutFile=%s, Start=%s, Length=%s") % (outfile, startsecs, lengthsecs))

	executecmd(('~/dev/vidscripts/trimvid.py  "%s" %s %s "%s"') % (sourcefilepath, startsecs, lengthsecs, outfile))

print "Done"



